package org.file_service.service.serviceimpl;

import org.file_service.dao.UserMapper;
import org.file_service.entity.User;
import org.file_service.lang.Result;
import org.file_service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;

    @Override
    @Transactional
    public Result login(String name, String password) {
        User user = userMapper.idIsLegal(name);
        System.out.println(user);
        if (user == null){
            return Result.fail("账号错误");
        }else {
            User user1 = userMapper.login(name, password);
            if (user1 == null){
                return Result.fail("密码错误");
            }else {
                return Result.suc(user);
            }
        }
    }

    @Override
    public Result enroll(String name, String password) {
        User user = userMapper.idIsLegal(name);
        if (user !=null ){
            return Result.fail("用户名存在！");
        }
        int res = userMapper.enroll(name, password);
        if (res != 0) {
            return Result.suc("用户注册成功");
        } else return Result.fail("用户注册失败");
    }
}
